package com.fourth.service.impl;

import com.fourth.dao.UbBusinessInfoDao;
import com.fourth.dao.UbGoodsDao;
import com.fourth.dao.UbStockDao;
import com.fourth.service.UbOrderPayService;
import com.fourth.util.GetUidUtil;
import com.ljx.entity.*;
import com.ljx.vo.Result;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author Mr.ma
 * @since 2023/3/17 16:31
 **/
@Service
public class UbOrderPayServiceImpl implements UbOrderPayService {

    @Resource
    private UbStockDao ubStockDao;

    @Resource
    private UbGoodsDao ubGoodsDao;

    @Resource
    private UbBusinessInfoDao ubBusinessInfoDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result settlement(List<Cart> cartList) {
        /* 判断购物车中的商品库存是否充足*/
        for (int i = 0; i < cartList.size(); i++) {
            // 执行业务
            Stock stock = ubStockDao.selectByGoodsId(cartList.get(i).getGoodsId());
            Long oldResidue = Long.valueOf(stock.getResidue());
            if (oldResidue == 0 || cartList.get(i).getNum() > oldResidue){
                return new Result(5000,"很抱歉，商品库存不足");
            }
        }
        // 创建所有商品订单的总金额
        BigDecimal orderTotal = BigDecimal.ZERO;
        // 获取公共信息
        String uid = GetUidUtil.getUid();
        // 设置订单code编号
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString();
        String code = uuidStr.replace("-", "");

        List<Order> orderList = new ArrayList<>();
        // 为每个商品都创建一个订单对象，共用一个code编号
        for (int i = 0; i < cartList.size(); i++) {
            Order order = new Order();
            // 设置订单创建时间
            order.setOrderdate(new Date());
            // 获取当前登录用户的id并保存到订单中
            order.setUid(uid);
            // 设置支付方式
            order.setPaysort(3);
            // 设置订单code编号
            order.setCode(code);
            // 设置发货状态
            order.setSend(0);
            // 设置评论状态
            order.setCommentstatus(0);
            // 设置支付状态
            order.setState(1);
            // 设置bid
            order.setBid(cartList.get(i).getGoods().getBid());
            // 设置goodsid
            order.setGoodsid(cartList.get(i).getGoodsId());
            // 设置num
            order.setNum(cartList.get(i).getNum());
            // 设置addrid
            order.setAddrid(cartList.get(i).getAddrid());
            // 设置每个商品对应的订单价格
            BigDecimal price = cartList.get(i).getPrice();
            order.setPrice(price);
            // 设置总金额
            BigDecimal total = price.multiply(new BigDecimal(String.valueOf(cartList.get(i).getNum())));
            order.setTotalmoney(total);


            // 加入一些收银台展示信息，商品对象与店铺对象
            Goods goods = ubGoodsDao.findById(Integer.valueOf(cartList.get(i).getGoodsId()));
            BusinessInfo businessInfo = ubBusinessInfoDao.selectByBid(cartList.get(i).getGoods().getBid());
            order.setGoods(goods);
            order.setBusinessInfo(businessInfo);
            orderList.add(order);
            // 保存到数据库中
//            ubOrderService.insert(order);
//            // 计算订单总金额
            orderTotal.add(total);
        }

        // 为每个订单添加上订单的总金额
        orderList.stream().forEach(item -> item.setTelephone(orderTotal.toString()));


        // 添加到延迟支付队列
        // msgProducer.createMst(code);
        return new Result(2000, "订单生成成功", orderList);
    }
}
